package com.zhongkun.datahome.service.res.appointment;

import com.zhongkun.datahome.service.domain.AppointmentTimeSlot;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author huangjie
 * @since 2025/6/27
 */
@Data
public class AppointmentTimeSlotExportVo {

    private String customerInfo;

    private String timeSlotInfo;

    private Date timeSlotDate;

    private Long appointmentId;

    private Long timeSlotId;

    public static List<AppointmentTimeSlotExportVo> build(List<AppointmentTimeSlot> appointmentTimeSlotItemList, Map<Long, List<AppointmentRecordVo>> recordSlotMap) {
        List<AppointmentTimeSlotExportVo> resList = new ArrayList<>();
        for (AppointmentTimeSlot appointmentTimeSlot : appointmentTimeSlotItemList) {
            AppointmentTimeSlotExportVo appointmentTimeSlotExportVo = new AppointmentTimeSlotExportVo();

            List<AppointmentRecordVo> appointmentRecordVoList = recordSlotMap.get(appointmentTimeSlot.getId());
            if (CollectionUtils.isEmpty(appointmentRecordVoList)) {
                continue;
            }
            String houseInfo = appointmentRecordVoList.stream().map(x -> MessageFormat.format("{0} {1} {2}({3}人)", x.getHouseName(), x.getCustomerName(), x.getCustomerPhone(), x.getVisitNum())).collect(Collectors.joining(","));

            appointmentTimeSlotExportVo.setAppointmentId(appointmentTimeSlot.getAppointmentId());
            appointmentTimeSlotExportVo.setCustomerInfo(houseInfo);
            appointmentTimeSlotExportVo.setTimeSlotInfo(MessageFormat.format("{0}-{1}", appointmentTimeSlot.getStartTime(), appointmentTimeSlot.getEndTime()));
            appointmentTimeSlotExportVo.setTimeSlotDate(appointmentTimeSlot.getAppointmentDate());

            appointmentTimeSlotExportVo.setTimeSlotId(appointmentTimeSlot.getId());
            resList.add(appointmentTimeSlotExportVo);
        }
        return resList;
    }
}
